combination object
This algorithm generates all combinations of a given size from a set, including repetitions in all possible orders.
bool generate_all_combinations(int items, int combination_size)
Parameters:
items
The number of items in the set. This must be at least 2.
combination_size
The number of items in each combination. Since we include repetitions, this must be at least 2 but can be greater than items.
Return value:
true on success, false on failure.
Remarks:
When enumerating combinations with two items in each from a set of three items, the nine possible combinations that this algorithm generates are:
[ 0, 0 ]
[ 0, 1 ]
[ 0, 2 ]
[ 1, 0 ]
[ 1, 1 ]
[ 1, 2 ]
[ 2, 0 ]
[ 2, 1 ]
[ 2, 2 ]
Example:
// We have a basket and an unlimited quantity of three types of fruit. The basket may only contain two fruits at any given time.
// Enumerate all the combinations of fruit that the basket could possibly contain.
// Note: This same algorithm can be used for other things such as getting all the possible outcomes of rock, paper and sizzors.
void main()
{
int combination_size=2;
// This is the number of items that we want in each combination.
int items=3;
// This is the number of items we have all in total, which is to say our different types of fruit.
combination process;
if(process.generate_all_combinations(items, combination_size)==false)
{
alert("Error", get_last_error_text());
exit();
}
int[] list;
// This is the array which will be filled with each combination.
int count=0;
// This simply keeps track of how many combinations we've generated so far. It is not really needed.
while(process.next(list))
{
count+=1;
// At this point, we have a new combination. We print it out in an alert box as a comma separated string.
string data;
for(int i=0;i<list.length();i++)
{
if(i>0)
data+=", ";
data+=list[i];
}
alert("Combination " + count, data);
}
// We're done, so let's display how many combinations we found before we quit.
alert("Result", count + " combinations were generated from a set of " + items + " items, with " + combination_size + " items in each combination.");
}